﻿
using System.Reflection;
using bpms.Models;
using bpms.Utils;
using MySql.Data.MySqlClient;

namespace bpms.Dao
{
    /// <summary>
    /// 管理员数据持久层
    /// </summary>
    public class AdminUserDao
    {
        /// <summary>
        /// 查询所有管理员用户
        /// </summary>
        /// <returns></returns>
        public List<AdminUser> GetAll()
        {
            // 查询管理员用户
            var adminList = MySqlDbHelper.QueryList<AdminUser>(
                @"SELECT * FROM admin_user;"
                );
            return adminList;
        }

        /// <summary>
        /// 根据Id查询管理员用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AdminUser GetById(int id)
        {
            // 查询管理员用户
            var admin = MySqlDbHelper.Query<AdminUser>(
                @"SELECT * FROM admin_user WHERE id = @id;",
                new MySqlParameter("@id", id)
                );
            return admin;
        }

        /// <summary>
        /// 根据登录名查询管理员用户
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public AdminUser GetByLoginName(string loginName)
        {
            // 查询管理员用户
            var admin = MySqlDbHelper.Query<AdminUser>(
                @"SELECT * FROM admin_user WHERE login_name = @loginName",
                new MySqlParameter("@loginName", loginName)
                );
            return admin;
        }

        /// <summary>
        /// 根据登录名和密码查询管理员用户
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public AdminUser GetAdminByLoginNameAndPwd(string loginName, string password)
        {
            password= PasswordHelper.MD5Encrypt(password); 
            // 查询管理员用户
            var admin = MySqlDbHelper.Query<AdminUser>(
                @"SELECT * FROM admin_user WHERE login_name = @loginName and password=@password;",
                new MySqlParameter("@loginName", loginName),
                new MySqlParameter("@password", password)
                );
            return admin;
        }

        /// <summary>
        /// 更新管理员用户
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public int update(AdminUser admin)
        {
            admin.Password = PasswordHelper.MD5Encrypt(admin.Password);
            const string sql = @"UPDATE admin_user SET
                                            name = @name,
                                            password = @password,
                                            phone = @phone,
                                            updated_time = now()
                                            WHERE id = @id";

            var parameters = new[] {
                                new MySqlParameter("@name", admin.Name),
                                new MySqlParameter("@password", admin.Password),
                                new MySqlParameter("@phone", admin.Phone),
                                new MySqlParameter("@id", admin.Id)
                    };

            int cnt = MySqlDbHelper.Execute(sql, parameters);
            return cnt;
        }

        /// <summary>
        /// 添加管理员用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(AdminUser model)
        {
            var insertSql =$@"
                        insert into admin_user(login_name,password,name,phone,created_time,updated_time) 
                        values(@LoginName,@Password,@Name, @Phone, now(),now());";
            var parameters = new[]
            {
                    new MySqlParameter("@LoginName", model.LoginName),
                    new MySqlParameter("@Password", PasswordHelper.MD5Encrypt(model.Password)),
                    new MySqlParameter("@Name", model.Name),
                    new MySqlParameter("@Phone", model.Phone),
                };
            int cnt = MySqlDbHelper.Execute(insertSql, parameters);
            return cnt;
        }
    }
}
